<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12" :xs="24">
        <el-card>
          <template #header>
            <!--            <div class="card-header">-->
            <span>代理商配置</span>
            <div class="btn">
              <el-button type="primary" size="mini" @click="onSubmitAgentSetting">充值</el-button>
              <el-button type="warning" size="mini" @click="subtractAgentSetting">减少</el-button>
            </div>
            <!--            </div>-->
          </template>
          <el-form
            ref="agentSettingDataForm"
            label-position="left"
            :model="agentSettingDataForm"
            label-width="140px"
          >
            <el-form-item label="代理金额/(元)">
              <el-input v-model="agentSettingDataForm.price" disabled/>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getAgentSetting, rechargeBalance, subtractBalance } from '@/api/admin/sysConfig'

export default {
  name: 'Recharge',
  data() {
    return {
      agentSettingDataForm: {
        name: undefined,
        price: undefined
      }
    }
  },
  async created() {
    this.getAgentSettingInfo()
  },
  methods: {
    subtractAgentSetting() {
      this.$prompt('请输入减少的金额', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern:
          /^\+?[1-9]\d*$/,
        inputErrorMessage: '减少金额不正确'
      })
        .then(({ value }) => {
          subtractBalance({ recharge: value }).then(res => {
            this.getAgentSettingInfo()
            this.$message({
              type: 'success',
              message: '你的减少金额是: ' + value
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
    },

    onSubmitAgentSetting() {
      this.$prompt('请输入充值金额', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        // ^(\-|\+)?\d+(\.\d+)?$
        inputPattern:
          /^\+?[1-9]\d*$/,
        inputErrorMessage: '充值金额不正确'
      })
        .then(({ value }) => {
          rechargeBalance({ recharge: value }).then(res => {
            this.getAgentSettingInfo()
            this.$message({
              type: 'success',
              message: '你的充值金额是: ' + value
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
    },
    getAgentSettingInfo() {
      getAgentSetting().then(res => {
        this.agentSettingDataForm = res.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.btn {
  display: flex;
  justify-content: space-between;
  flex-direction: row;
  justify-content: flex-end;
}

</style>
